package com.gin.no_gay_alliance.database.entity;

import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.Index;
import androidx.room.PrimaryKey;

import com.gin.nga.enums.EntranceType;
import com.gin.nga.enums.ReplyStatus;
import com.gin.no_gay_alliance.viewmodel.TopicItem;

import java.time.ZonedDateTime;

/**
 * 主题浏览历史
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/9/15 14:06
 **/
@Entity(indices = {@Index(name = "timestamp", value = "timestamp", unique = true)})
public class TopicHistory {
    /**
     * 主题id
     */
    @PrimaryKey
    public long topicId;
    /**
     * 作者用户ID 可能为匿名
     */
    public String authorId;
    /**
     * 作者用户名
     */
    public String authorName;
    /**
     * 标题
     */
    public String title;
    /**
     * 最后回复用户名
     */
    public String lastPosterName;
    /**
     * 回复数
     */
    public int replyCount;
    /**
     * 单帖回复
     */
    public boolean replyOnce;
    /**
     * 由回复数计算得来的最大页数
     */
    public int totalPage;
    /**
     * 标题颜色
     */
    public int color;
    /**
     * 加粗
     */
    public boolean bold;
    /**
     * 删除线
     */
    public boolean lineThrough;
    /**
     * 斜体
     */
    public boolean italic;
    /**
     * 主题状态，用以复原状态数据
     */
    public int type;
    /**
     * 发布时间
     */
    public ZonedDateTime postDateTime;
    /**
     * 最后回复时间
     */
    public ZonedDateTime lastPost;
    /**
     * 浏览时间
     */
    public long timestamp;
    /**
     * 上次浏览到的页数
     */
    @ColumnInfo(defaultValue = "1")
    public int page;

    public TopicHistory(TopicItem that) {
        this.topicId = that.topicId;
        this.title = that.title;
        this.authorName = that.authorName;
        this.replyCount = that.replyCount;
        this.replyOnce = that.replyOnce;
        this.totalPage = that.totalPage;
        this.color = that.color;
        this.bold = that.bold;
        this.lineThrough = that.lineThrough;
        this.italic = that.italic;
        this.type = that.type;
        this.authorId = that.authorId;
        this.lastPosterName = that.lastPosterName;

        this.postDateTime = that.postDateTime;
        this.lastPost = that.lastPost;

        this.timestamp = System.currentTimeMillis();
    }

    public TopicHistory() {
    }

    public TopicItem toTopicItem() {
        TopicItem that = new TopicItem();
        that.topicId = this.topicId;
        that.title = this.title;
        that.authorName = this.authorName;
        that.replyCount = this.replyCount;
        that.replyOnce = this.replyOnce;
        that.totalPage = this.totalPage;
        that.color = this.color;
        that.bold = this.bold;
        that.lineThrough = this.lineThrough;
        that.italic = this.italic;
        that.type = this.type;
        that.authorId = this.authorId;
        that.lastPosterName = this.lastPosterName;

        that.postDateTime = this.postDateTime;
        that.lastPost = this.lastPost;

        // 状态
        that.status = ReplyStatus.parse(this.type);
        that.entranceType = EntranceType.TOPIC;

        return that;
    }
}
